/*******************************************************************************
 * Copyright (c) 2007 Benjamin Muskalla and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Benjamin Muskalla - initial API and implementation
 *******************************************************************************/
package de.bmuskalla.brainfuck.ui.launch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;

import de.bmuskalla.brainfuck.model.BFDebugTarget;
import de.bmuskalla.brainfuck.ui.BFLaunchPlugin;

public class BFLaunchConfigurationDelegate extends LaunchConfigurationDelegate {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration,
	 *      java.lang.String, org.eclipse.debug.core.ILaunch,
	 *      org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void launch(ILaunchConfiguration configuration, String mode,
			ILaunch launch, IProgressMonitor monitor) throws CoreException {

		List commandLine = new ArrayList();
		commandLine.add("java");
		commandLine.add("-classpath");
		commandLine.add(BFLaunchPlugin.INTERPRETER_PATH);
		commandLine.add("de.bmuskalla.brainfuck.Brainfuck");
		if (mode.equals(ILaunchManager.DEBUG_MODE)) {
			commandLine.add("-debug");
		}

		String fileName = "/home/benny/foo.bf";
		commandLine.add(fileName);

		try {
			Process p = Runtime.getRuntime().exec(
					(String[]) commandLine.toArray(new String[0]));
			IProcess process = DebugPlugin.newProcess(launch, p,
					"Brainfuck interpreter");

			if (mode.equals(ILaunchManager.DEBUG_MODE)) {
				launch.addDebugTarget(new BFDebugTarget(process, launch));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
